//
//  ZHTabBarViewController.m
//  ZHLifeCircle
//
//  Created by user on 15/11/21.
//  Copyright © 2015年 heima. All rights reserved.
//

#import "ZHTabBarViewController.h"
#import "ZHToolsViewController.h"
#import "ZHCircleViewController.h"
#import "ZHHomeViewController.h"
#import "ZHOnlineViewController.h"
#import "ZHUsViewController.h"
#import "ZHShakeViewController.h"
#import "ZHHotsViewController.h"
#import "ZHMoreViewController.h"




#define viewControllerCount 5
#define topViewControllerCount 5
#define tabBarHeight 49
#define topViewHeight 44

@interface ZHTabBarViewController ()

@property (nonatomic, strong) NSMutableArray *buttons;
@property (nonatomic, strong) NSMutableArray *controllers;
@property (nonatomic, strong) UIView *topView;

@end

@implementation ZHTabBarViewController

- (NSMutableArray *)controllers{
    if (_controllers == nil) {
        NSArray *controllerString = @[@"ZHHomeViewController", @"ZHHotsViewController", @"ZHCircleViewController", @"ZHToolsViewController", @"ZHMoreViewController", @"ZHShakeViewController", @"ZHOnlineViewController", @"ZHUsViewController"];
        
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        NSMutableArray *tempArray = [[NSMutableArray alloc] init];
        [controllerString enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString *name = (NSString *)obj;
            Class VC = [storyBoard instantiateViewControllerWithIdentifier:name];
            UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:VC];
            //为什么要讲NAVController添加进入数组,这不是相当于创建了多个NAVController吗？
            nav.navigationBar.hidden = YES;
            [tempArray addObject:nav];
            
        }];
        _controllers = tempArray;
    }
    return _controllers;
}

- (NSMutableArray *)buttons{
    if (!_buttons) {
        _buttons = [[NSMutableArray alloc] init];
    }
    return _buttons;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.viewControllers = self.controllers;
    UIView *bottmView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - tabBarHeight, kScreenWidth, tabBarHeight)];
    //添加btn
    for(int i = 0; i < viewControllerCount; i ++){
        CGFloat width = kScreenWidth / 5.0;
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(i * width, 0, width, tabBarHeight)];
        btn.tag = i;
        //拼接字符串
        NSString *norlImage = [NSString stringWithFormat:@"home_%d",i];
        NSString *selectedImage = [NSString stringWithFormat:@"home_%d_pressed",i];
        [btn setBackgroundImage:[UIImage imageNamed:norlImage] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(enterViewController:) forControlEvents:UIControlEventTouchUpInside];
        [bottmView addSubview:btn];
        [self.buttons addObject:btn];
    }
    bottmView.backgroundColor = [UIColor blackColor];
    
    [self.view addSubview:bottmView];
    
}
- (void)enterViewController:(UIButton *)btn{
    
    [self.buttons enumerateObjectsUsingBlock:^(UIButton *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.selected = NO;
    }];
    btn.selected = YES;
    if (btn.tag < 4) {
        self.topView.hidden = YES;
        //进入控制器
        self.selectedIndex = btn.tag;
    }
    else if(btn.tag == 4){
        //添加View
        UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - tabBarHeight - topViewHeight, kScreenWidth, topViewHeight)];
        self.topView = topView;
        UIImageView *bg_View = [[UIImageView alloc] initWithFrame:topView.bounds];
        bg_View.image = [UIImage imageNamed:@"home_topbar"];
        [topView addSubview:bg_View];
        //添加btn
        [self addtopViewBtns:topView];
        [self.view addSubview:topView];
    }
    else{
        //跳转控制器
        self.selectedIndex = btn.tag;
    }
}

- (void)addtopViewBtns:(UIView *)topView{
    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth * (94 / 375.0), topViewHeight)];
    button1.tag = 5;
    [button1 setTitle:@"摇一摇" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(enterViewController:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(button1.frame), 0, kScreenWidth * (104 / 375.0), topViewHeight)];
    button2.tag = 6;
    [button2 setTitle:@"关于我们" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(enterViewController:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button3 = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(button2.frame), 0, kScreenWidth * (79 / 375.0), topViewHeight)];
    button3.tag = 7;
    [button3 setTitle:@"直播" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(enterViewController:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *button4 = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMaxX(button3.frame), 0, kScreenWidth * (98 / 375.0), topViewHeight)];
    button4.tag = 8;
    [button4 setTitle:@"周边" forState:UIControlStateNormal];
    [button4 addTarget:self action:@selector(enterViewController:) forControlEvents:UIControlEventTouchUpInside];
    
    [topView addSubview:button1];
    [topView addSubview:button2];
    [topView addSubview:button3];
    [topView addSubview:button4];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
